<?php

class Damages_Methods_Request_Edit_ExpirationsDicts extends Damages_Methods_Request {

  /**
   *
   * @var Damages_Methods_Data_Dictionary_Expiration_Edit[] 
   */
  public $expirations = array();

  public function __construct($params) {
    parent::__construct($params);
    foreach ($params->expirations->toArray() as $expiration) {
      $this->expirations[] = new Damages_Methods_Data_Dictionary_Expiration_Edit($expiration);
    }
  }

  /**
   * 
   * @return Damages_Methods_Response_GetExpirationsDicts
   */
  public function process() {
    if (!Damages_Auth::getInstance()->checkPermission(Damages_Auth::PERMISSION_ADMIN))
      throw new Damages_Exception_App(Damages_Codes::ACCESS_DENIED);
    $con = Propel::getConnection(Damages_Config::getInstance()->datasource);

    try {
      $con->beginTransaction();
      foreach ($this->expirations as $expiration) {
        $dbObj = DamagesDictExpirationsQuery::create()->findOneById($expiration->id);
        $dbObj->setDays($expiration->days);
        $dbObj->setHours($expiration->hours);
        $dbObj->save();
      }
      $con->commit();
    } catch (Exception $exc) {
      $con->rollBack();
      throw $exc;
    }

    $response = new Damages_Methods_Response(true);
    return $response;
  }

}